package com.doctor.windflower_doctor.recordfragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.delegate.GetPositionDelegate;
import com.doctor.windflower_doctor.entity.RecordBeen;
import com.doctor.windflower_doctor.fragment.BaseFragment;
import com.doctor.windflower_doctor.http.parser.impl.JsonHelper;
import com.doctor.windflower_doctor.myuser.tools.HttpclientHelper;
import com.doctor.windflower_doctor.util.DateUtils;
import com.doctor.windflower_doctor.util.StringUtil;
import com.doctor.windflower_doctor.view.LineView;

@SuppressLint("SimpleDateFormat")
public class FetalMoveChartFragment extends BaseFragment {
	private static FetalMoveChartFragment baseFragment;
	private List<RecordBeen> notesList;
	private LineView lineView;
	// private SharedPreferencesDB sharedPreferencesDB;
	private ArrayList<String> monthList;
	private HorizontalScrollView horizontalScrollView;
	private final String weightURL = "http://192.168.199.162:3011/api/notes/v2/quicken?userid=543a374d623e8b2d2652e546";
	private Time time;

	public static FetalMoveChartFragment getInstance(Bundle bundle) {
		baseFragment = new FetalMoveChartFragment();
		baseFragment.setArguments(bundle);
		return baseFragment;
	}

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_weight_chart, container,
				false);
	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
	}

	@Override
	protected void findViews(View view) {
		time = new Time("GMT+8");
		time.setToNow();
		monthList = new ArrayList<String>();
		lineView = (LineView) view.findViewById(R.id.line_view);
		lineView.setDrawDotLine(true);
		lineView.setShowPopup(LineView.SHOW_POPUPS_NONE);
		horizontalScrollView = (HorizontalScrollView) view
				.findViewById(R.id.horizontalScrollView);
		new WeightAsynck().execute(weightURL);
	}

	private void initBottomTextList() {
		lineView.setBottomTextList(getMonthDaysList());
	}
	String getBegin() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateBegin = time.year +"-"+ (time.month + 1)+"-"
				+ time.monthDay + "";
		
		Date date = null;
		try {
			date = dateFormat.parse(dateBegin);
		} catch (ParseException e) {

			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		Date dateFirst = new DateUtils(getActivity()).getFirstDay(date.getTime());
		return dateFormat.format(dateFirst);
	}
	private void initDate(List<RecordBeen> notesList) {
		ArrayList<ArrayList<String>> dataLists = new ArrayList<ArrayList<String>>();

		ArrayList<String> strList = new ArrayList<String>();
		for (int i = 0, p = monthList.size(); i < p; i++) {
			strList.add("0");
			String current = monthList.get(i);
			if (notesList != null && notesList.size() > 0)
				for (int j = 0, k = notesList.size(); j < k; j++) {
					if (getMonthDay(Long.valueOf(notesList.get(j).getCreatedAt())).equals(
							current)) {
						strList.remove(i);
						if ("null".equals(notesList.get(j).getFetalMovement())) {
							notesList.get(j).setFetalMovement("0");
						}
						strList.add((StringUtil.isEmpty(notesList.get(j)
								.getFetalMovement())) ? "0" : notesList.get(j)
								.getFetalMovement());
					}
				}
		}
		dataLists.add(strList);

		lineView.setDataList(dataLists);
		lineView.setGetPositionDelegate(new GetPositionDelegate() {

			@Override
			public void getnum(int num) {
				horizontalScrollView.scrollTo(num, 0);
			}
		});
	}

	@Override
	public void onResume() {
		// if (getActivity().getRequestedOrientation() !=
		// ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
		// getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		// }

		super.onResume();
	}

	@SuppressWarnings("deprecation")
	public ArrayList<String> getMonthDaysList() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
		
		try {
			Date begin = format.parse(getBegin());
			Date end = new Date();
			int months = (end.getYear() - begin.getYear()) * 12
					+ (end.getMonth() - begin.getMonth());
			Calendar calendarbegin = Calendar.getInstance();
			calendarbegin.setTime(begin);
			int daybegin = calendarbegin.get(Calendar.DAY_OF_MONTH);
			int daysum = calendarbegin.getActualMaximum(Calendar.DAY_OF_MONTH);
			if (daybegin > 0)
				for (int i = daybegin; i <= daysum; i++) {
					if (i < 10) {
						monthList.add(monthFormat.format(calendarbegin
								.getTime()) + "-0" + i);
					} else {
						monthList.add(monthFormat.format(calendarbegin
								.getTime()) + "-" + i);
					}
				}
			for (int i = 1; i < months; i++) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(begin);
				calendar.add(Calendar.MONTH, i);
				for (int j = 1; j <= calendar
						.getActualMaximum(Calendar.DAY_OF_MONTH); j++) {
					if (j < 10) {
						monthList.add(monthFormat.format(calendar.getTime())
								+ "-0" + j);
					} else {
						monthList.add(monthFormat.format(calendar.getTime())
								+ "-" + j);
					}
				}
			}
			Calendar calendar1 = Calendar.getInstance();
			calendar1.setTime(end);
			int days = calendar1.get(Calendar.DAY_OF_MONTH);
			if (days > 0)
				for (int i = 1; i <= days; i++) {
					if (i < 10) {
						monthList.add(monthFormat.format(calendar1.getTime())
								+ "-0" + i);
					} else {
						monthList.add(monthFormat.format(calendar1.getTime())
								+ "-" + i);
					}
				}

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return monthList;
	}

	@SuppressLint("SimpleDateFormat")
	String getMonthDay(long string) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
		Date date = new Date(string);
		return dateFormat.format(date);
	}

	@Override
	public void onStop() {
		baseFragment = null;
		super.onStop();

	}

	@Override
	public void onDestroyView() {
		baseFragment = null;
		super.onDestroyView();

	}

	class WeightAsynck extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return HttpclientHelper.getAddress(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(result!=null){
				notesList = JsonHelper.record_Json(result);
				initBottomTextList();
				initDate(notesList);
			}else{
				Toast.makeText(getActivity(), "请检查网络连接", Toast.LENGTH_SHORT).show();
			}
			
			
		}
	}
}
